package com.filyar.htyd.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.filyar.htyd.common.Result;
import com.filyar.htyd.entity.Az;
import com.filyar.htyd.service.IAzService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author filyar
 * @e-mail root@filyar.com
 * @since 2024-10-24
 */
@RestController
@RequestMapping("/az")
public class AzController {

    @Resource
    private IAzService azService;

    private final String now = DateUtil.now();

    @GetMapping("/getAzCount")
    public Result getAzCount() {
        return Result.success(azService.count());
    }

    @PostMapping("/check")
    public Result check(@RequestBody Az az) {
        return Result.success(azService.check(az));
    }

    @GetMapping("/compare")
    public Result compare() {
        azService.compare();
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Az az) {
        azService.saveOrUpdate(az);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        azService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        azService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(azService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String a,
                           @RequestParam(defaultValue = "") String z,
                           @RequestParam(defaultValue = "") String addressCode,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        LambdaQueryWrapper<Az> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!a.isEmpty()) {
            lambdaQueryWrapper.like(Az::getA, a);
        }
        if (!z.isEmpty()) {
            lambdaQueryWrapper.like(Az::getZ, z);
        }
        if (!addressCode.isEmpty()) {
            lambdaQueryWrapper.like(Az::getAddressCode, addressCode);
        }
        return Result.success(azService.page(new Page<>(pageNum, pageSize), lambdaQueryWrapper));
    }


}

